package com.example.onlinemarket.graphics;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.example.onlinemarket.R;
import com.example.onlinemarket.activity.MainActivity;

import java.util.HashMap;
import java.util.Map;

import static com.example.onlinemarket.Application.loginUser;
import static com.example.onlinemarket.util.ActivityUtil.contextActivity;
import static com.example.onlinemarket.util.HttpUtil.sendHttpRequest;

public class UpdatePwdDialog extends AlertDialog {

    private TextView titleTv;
    private EditText originPwdEt;
    private EditText newPwdEt;
    private EditText repeatPwdEt;
    private TextView tipTv;
    private Button submitBtn;

    protected UpdatePwdDialog(@NonNull Context context) {
        super(context);
    }

    public static UpdatePwdDialog newInstance(@NonNull Context context) {
        UpdatePwdDialog dialog = new UpdatePwdDialog(context);
        dialog.setCancelable(true);
        dialog.create();
        dialog.titleTv.setText("更改密码");
        return dialog;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_update_password);
        //初始化组件
        initWidgets();
        //设置事件监听器
        setWidgetsListener();
    }

    //设置事件监听器
    private void setWidgetsListener() {
        submitBtn.setOnClickListener(v -> {
            String pwd = originPwdEt.getText().toString().trim();
            if(loginUser.getPassword().equals(pwd)){
                String newPwd = newPwdEt.getText().toString().trim();
                if(newPwd.equals(pwd)){
                    tipTv.setText("新旧密码不能相同");
                    submitBtn.setEnabled(false);
                }else {
                    dismiss();
                    new Thread(() -> {
                        loginUser.setPassword(newPwd);
                        Map<String, String> map = new HashMap<>();
                        map.put("userName", loginUser.getUserName());
                        map.put("password", newPwd);
                        sendHttpRequest("/update/user/password", "POST", map);
                    }).start();
                    ((MainActivity)contextActivity).passwordChanged();
                }
            }else {
                tipTv.setText("输入的原密码错误");
                submitBtn.setEnabled(false);
            }
        });
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) { }
            @Override
            public void afterTextChanged(Editable s) {
                String originPwdText = originPwdEt.getText().toString().trim();
                if(originPwdText.isEmpty()) {
                    tipTv.setText("请输入原密码");
                    submitBtn.setEnabled(false);
                }
                else{
                    String newPwdText = newPwdEt.getText().toString().trim();
                    String repeatPwdText = repeatPwdEt.getText().toString().trim();
                    if(newPwdText.isEmpty()&&repeatPwdText.isEmpty()){
                        tipTv.setText("新密码不能为空");
                        submitBtn.setEnabled(false);
                    }
                    else if(!newPwdText.isEmpty()&&repeatPwdText.isEmpty()) {
                        tipTv.setText("请再次确认密码");
                        submitBtn.setEnabled(false);
                    }
                    else if(!newPwdText.equals(repeatPwdText)) {
                        tipTv.setText("两次输入的密码不一致");
                        submitBtn.setEnabled(false);
                    }
                    else {
                        tipTv.setText("");
                        submitBtn.setEnabled(true);
                    }
                }
            }
        };
        repeatPwdEt.addTextChangedListener(watcher);
        newPwdEt.addTextChangedListener(watcher);
        originPwdEt.addTextChangedListener(watcher);
    }

    //初始化组件
    private void initWidgets() {
        titleTv = findViewById(R.id.dialog_title);
        tipTv = findViewById(R.id.dialog_tip);
        newPwdEt = findViewById(R.id.et_dialog_new_pwd);
        originPwdEt = findViewById(R.id.et_dialog_origin_pwd);
        repeatPwdEt = findViewById(R.id.et_dialog_repeat_pwd);
        submitBtn = findViewById(R.id.btn_dialog_submit);
    }
}
